/* Let a[i][j] be the initial state of the board, where a[i][j] = 1 means
 * the square is 'on', and a[i][j] = 0 means the square is 'off'. Our task
 * is to turn all squares 'on' or 'off' with the fewest steps.
 *
 * Let x[i][j] be 1 if we need to flip square (i, j), or 0 if we don't flip
 * it. Then for each a[u][v], we have the following equation:
 *     sum (1 <= i <= 5, 1 <= j <= 6) c[i][j]*x[i][j] == a[u][v] (mod 2),
 * where c[i][j] = 1 if and only if (i, j) and (u, v) are equal or adjacent.
 *
 * This is a system of 30 linear equations which can be solved by Gaussian
 * elimination. Below we first use symbolic computation to find the solution
 * as the XOR of certain a[i][j]'s. Then we convert this symbolic solution
 * into C source code to solve the problem quickly.
 */

#include <stdio.h>

#define ROWS 5
#define COLS 6

#define SYMBOLIC 0              /* 1: symbolic; 0: numerical */
typedef unsigned long mask_t;   /* bit-mask of coefficients: 1=on, 0=off */

/* Count the number of bits set in x. */
static int popcnt(mask_t x)
{
    int count = 0;
    while (x)
    {
        ++count;
        x &= (x - 1);
    }
    return count;
}

#if SYMBOLIC
typedef struct equation
{
    mask_t coeff;   /* bit-mask of coefficients: 1=on, 0=off */
    mask_t target;  /* bit-mask of the a[i][j]s to XOR */
} equation;

int main()
{
    int i, j, k;
    equation eq[ROWS*COLS];
    mask_t one = 1;

    /* Build equations according to the problem setup. */
    for (i = 0; i < ROWS; i++)
        for (j = 0; j < COLS; j++)
        {
            k = i * COLS + j;
            eq[k].target = one << k;
            eq[k].coeff = one << k;
            if (i > 0)
                eq[k].coeff |= one << (k - COLS);
            if (i < ROWS - 1)
                eq[k].coeff |= one << (k + COLS);
            if (j > 0)
                eq[k].coeff |= one << (k - 1);
            if (j < COLS - 1)
                eq[k].coeff |= one << (k + 1);
        }

    /* Solve the equations using Gaussian elimination. */
    for (i = 0; i < ROWS*COLS; i++)
    {
        /* Find the first equation with a non-zero coefficient in x[i]. */
        for (k = i; k < ROWS*COLS; k++)
        {
            if (eq[k].coeff & (one << i))
                break;
        }
        if (k == ROWS*COLS) /* x[i] is free variable */
            continue;
        if (k != i)         /* swap equation i and k */
        {
            equation tmp = eq[i];
            eq[i] = eq[k];
            eq[k] = tmp;
        }

        /* Eliminate x[i] from all the subsequent equations. */
        for (k = i + 1; k < ROWS*COLS; k++)
            if (eq[k].coeff & (one << i))
            {
                eq[k].coeff ^= eq[i].coeff;
                eq[k].target ^= eq[i].target;
            }
    }

    /* Back-substitute to keep only one term for each equation. */
    for (i = ROWS*COLS-1; i >= 0; i--)
    {
        mask_t x = (eq[i].coeff & (one << i))? eq[i].target : (one << i);
        for (k = i - 1; k >= 0; k--)
        {
            if (eq[k].coeff & (one << i))
            {
                eq[k].coeff ^= eq[i].coeff;
                eq[k].target ^= x;
            }
        }
    }

    /* Display solution. */
    for (i = 0; i < ROWS*COLS; i++)
    {
        if (eq[i].coeff & (one << i))
            printf("x[%d][%d] = ", i / COLS + 1, i % COLS + 1);
        else
            printf("      0 = ");
        printf("0x%08x (LSB -> ", eq[i].target);

        for (k = 0; k < ROWS*COLS; k++)
            printf("%c", (eq[i].target & (one << k))? '1' : '0');
        printf(" <- MSB)\n");
    }

    return 0;
}
#else
static mask_t solve(mask_t initial)
{
    static const mask_t solutions[30] = {
        0x0310d578,0x0729c82c,0x0e57b176,0x1cab7a1b,0x3850e04d,0x3022ca87,
        0x04391d55,0x0a6eac20,0x15d50345,0x2aac2b28,0x14d950c1,0x08722aea,
        0x0d47644d,0x1cab7a9c,0x3b40343b,0x370b00b7,0x0e57b54e,0x2c89b8ac,
        0x15d51384,0x20c2a6ca,0x05350d55,0x28b02aea,0x010d5d94,0x2aae2708,
        0x3854d103,0x2c81c287,0x3617b54e,0x1ba37a9c,0x0d04e538,0x07aac230
    };
    int i;
    mask_t x = 0;
    for (i = 0; i < ROWS*COLS; i++)
    {
        mask_t b = popcnt(solutions[i] & initial) & 1;
        x |= (b << i);
    }
    return x;
}

int main()
{
    int T, t;

    scanf("%d", &T);
    for (t = 1; t <= T; t++)
    {
        mask_t initial = 0, solution;
        int i, j;
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
            {
                int a;
                scanf("%d", &a);
                initial |= (mask_t)a << (i * COLS + j);
            }
        }

        solution = solve(initial);
        printf("PUZZLE #%d\n", t);
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
            {
                if (j > 0)
                    printf(" ");
                printf("%c", (solution & (1 << (i * COLS + j)))? '1' : '0');
            }
            printf("\n");
        }
    }
    return 0;
}
#endif
